import Util from '../../utils/util.js';
import {applySupplier} from "../../api/api";

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fields: {
      mobile: {
        title: '联系方式',
        required: true,
        placeholder: "请填写您的手机号码",
        default_val: "",
      },
    },
    btnActive: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    Util.chekWxLogin().then(res => {
      wx.hideLoading()
      that.setData({
        userInfo: res.userInfo,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  formSubmit(e) {
    const that = this;
    const form_data = e.detail.value;
    const fields = that.data.fields;

    let msg = "";
    for (const k in fields) {
      if (fields[k].isShow) continue;

      if (fields[k].required && !form_data[k]) {
        fields[k].focus = true;
        msg = fields[k].title + "必填";
        this.setData({
          fields: fields
        })
        break;
      }
    }

    if (msg) {
      this.setData({btnActive: true})
      return Util.Tips({title: msg, hideMask: true});
    }

    if (!this.data.btnActive) {
      return Util.Tips({title: "不要重复提交", hideMask: true});
    }
    this.setData({btnActive: false})
    wx.showLoading({
      title: '正在提交..',
    })
    applySupplier(form_data).then(res => {
      this.setData({btnActive: true})
      return Util.Tips({
        title: '提交成功' || res.msg,
        icon: "success",
        endtime: 1500
      }, {tab: 3})
    }).catch(errmsg => {
      this.setData({btnActive: true})
      return Util.Tips({title: errmsg});
    });

  },

  inputName(e) {
    const that = this, fields = this.data.fields;
    const id = e.currentTarget.id;
    fields[id].default_val = e.detail.value;
    this.setData({fields: fields});
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },


  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})